<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="UTF-8">
    <title>请求日志</title>
    <link rel="shortcut icon" href="/res/img/favicon.ico" type="image/x-icon">
    <link href="/res/layui/css/layui.css" rel="stylesheet">
    <link href="/res/css/common.css" rel="stylesheet">
</head>
<body class="layui-anim layui-anim-downbit" style="height: 100%">
<div class="layui-form flex-row">
    <div class="layui-input-wrap" style="width: 200px">
        <label class="layui-form-label">端口</label>
        <div class="layui-input-block">
            <select id="port" name="port">
                <#list ports! as port>
                    <option value="${port}">${port}</option>
                </#list>
                <option value="">全部</option>
            </select>
        </div>
    </div>
    <div class="layui-input-wrap" style="width: 240px">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <select name="sort">
                <option value="desc">流量降序</option>
                <option value="asc">流量升序</option>
                <option value="">默认</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="width: 265px">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input autocomplete="off" type="text" class="layui-input" name="start" id="start" placeholder="开始时间">
        </div>
    </div>
    <div class="layui-form-item" style="width: 265px">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input autocomplete="off" type="text" class="layui-input" name="end" id="end" placeholder="结束时间">
        </div>
    </div>
    <div class="flex-row" style="margin-left: 20px">
        <button class="layui-btn" lay-submit lay-filter="lay-search"><i class="layui-icon layui-icon-search"></i>搜索
        </button>
        <button id="export" class="layui-btn layui-btn-primary"><i class="layui-icon layui-icon-export"></i>归档数据
        </button>
    </div>
</div>

<table id="tab" lay-filter="tab"></table>

<script src="/res/js/request.js"></script>
<script src="/res/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="see">查看详情</a>
    </div>
</script>
<script>
    layui.use(['table', 'dropdown'], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery
        var maxLength = 0

        // 创建渲染实例
        table.render({
            elem: '#tab',
            request: {
                pageName: 'page', // 页码的参数名称，默认：page
                limitName: 'size' // 每页数据条数的参数名，默认：limit
            },
            where: {sort: 'desc', port: $('#port').val()},
            url: '/api/accessList', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) { // res 即为原始返回的数据
                maxLength = res.total
                return {
                    "code": res.code, // 解析接口状态
                    "msg": res.msg, // 解析提示文本
                    "count": res.total, // 解析数据长度
                    "data": res.data // 解析数据列表
                };
            },
            height: '500', // 最大高度减去其他容器已占有的高度差
            // cellMinWidth: 80,
            page: true,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', fixed: 'left', width: 160, title: 'ID', sort: true},
                {field: 'type', width: 80, title: '类型', sort: true},
                {field: 'source', title: '来源', width: 150, sort: true},
                {field: 'target', title: '目标', width: 250, sort: true},
                {
                    field: 'size', title: '流量统计', width: 140, templet: function (row) {
                        return outSize(row.total)
                    }
                },
                {field: 'number', title: '访问次数', width: 110, sort: true},
                {field: 'updateTime', title: '最近访问时间', width: 180},
                {fixed: 'right', title: '操作', minWidth: 150, toolbar: '#barDemo'}
            ]]
        })
        // 触发单元格工具事件
        table.on('tool(tab)', function (obj) {
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'see') {
                layer.open({
                    title: '详情 - id:' + data.id,
                    type: 1,
                    area: ['80%', '80%'],
                    content: '<div class="flex-column" style="padding: 5px;">' +
                        '<h2>类型</h2>' +
                        '<p style="margin-top: 5px">' + data.type + '</p>' +
                        '<h2>总流量</h2>' +
                        '<p style="margin-top: 5px">' + outSize(data.total) + '</p>' +
                        '<h2>时间</h2>' +
                        '<p style="margin-top: 5px">' + data.createTime + '</p>' +
                        '<h2>内容</h2>' +
                        '<textarea style="margin-top: 5px;height: 180px;font-size: 14px">' + JSON.stringify(data, null, 2) + '</textarea>' +
                        '</div>'
                });
            }
        });

        // 提交事件
        form.on('submit(lay-search)', function (data) {
            layer.msg('加载中', {icon: 16, shade: 0.3, time: 300});
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            // layer.alert(JSON.stringify(field), {
            //     title: '当前填写的字段值'
            // });
            table.reloadData('tab', {
                where: field, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
            });
        });

        $('#export').click(function () {
            layer.prompt({
                title: '归档数据', formType: 0, maxlength: maxLength, minLength: 0,
                success: function (layero, index, that) {
                    var input = $(layero).find('input');
                    input.attr('placeholder', '归档数据的行数, 最大值 ' + maxLength)
                    input.css('margin-bottom', '10px')
                    input.after(`<b style="color: blue;">归档数据后，将以json形式保存到 conf/export 目录下。</b>`)
                }
            }, function (number, index) {
                if (number < 1) {
                    layer.msg('归档数据不能小于 1')
                    return
                }

                var index_ = layer.msg('请等待', {icon: 16, shade: 0.6, time: 10000});
                request.post('/api/accessExport', {
                    data: {number: number},
                    success(res) {
                        layer.close(index);
                        layer.msg(res.msg, {icon: 1});
                        setTimeout(function () {
                            window.location.reload()
                        }, 1000)
                    },
                    finally() {
                        layer.close(index_);
                    }
                })
            });
        })

        // 渲染
        laydate.render({
            elem: '#start',
            type: 'datetime'
        });
        laydate.render({
            elem: '#end',
            type: 'datetime'
        });
    })

    function outSize(bytes) {
        if (bytes < 1024)
            return bytes + ' byte'
        else if (bytes < 1048576)
            return bytesToKB(bytes)
        return bytesToMB(bytes)
    }

    function bytesToKB(bytes) {
        return (bytes / 1024).toFixed(2) + ' kb'; // toFixed(2) 用于保留两位小数
    }

    function bytesToMB(bytes) {
        return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
    }

</script>
</body>
</html>